package cn.edu.ctbu.myblog.dao;

import cn.edu.ctbu.myblog.domain.ArticleDO;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import tk.mybatis.mapper.common.Mapper;

import java.util.Date;
import java.util.List;

@org.apache.ibatis.annotations.Mapper
public interface ArticleDAO extends Mapper<ArticleDO>{
    @Select("SELECT\n" +
            "article.id,\n" +
            "article.title,\n" +
            "article.content,\n" +
            "article.good,\n" +
            "article.createTime,\n" +
            "article.userId,\n" +
            "`user`.userName\n" +
            "FROM\n" +
            "article\n" +
            "LEFT JOIN `user` ON `user`.id = article.userId")
    public List<ArticleDO> findAll();

    @Select("select * from article where id=#{id}")
    public List<ArticleDO> findById(int id);

    @Insert("insert into article (title,content,good,createTime,userId) values (#{title},#{content},#{good},#{createTime},#{userId})")
    public void addArticle(String title,String content,int good,Date createTime,int userId);

    @Update("update article set good=good+1 where id=#{id}")
    public void giveGood(int id);

    @Select("select * from article where title=#{title}")
    public List<ArticleDO> findByTitle(String title);
}